import axios from 'axios'

const token = localStorage.getItem('token') || sessionStorage.getItem('token') || ''

const service = axios.create({
  baseURL: 'http://jsonplaceholder.typicode.com/'
})

service.interceptors.request.use(config => {
  config.headers = {
    'X-Token': token
  }
  return config
})

service.interceptors.response.use(resp => {
  if (resp.status >= 200 && resp.status < 300) {
    return resp.data
  }

  return Promise.reject(resp)
})

export default service
